package com.cyandpos.pad.bankpay.bankImpl;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;

import com.cyandpos.pad.bankpay.BankFunctionConstant;
import com.cyandpos.pad.bankpay.IBankManager;
import com.cyandpos.pad.bankpay.P_BankData;
import com.cyandpos.pad.bankpay.R_BankData;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.MoneyUtils;
import com.cyandpos.pad.utils.log.MessageLog;

/**
 * Created by xin on 2021/7/28
 * desc: 中国银行(北京)
 */
public class BOC_BeiJing_Function implements IBankManager {

    public static final String packageName = "cn.boc.beijing.and.aolaiintranet";
    private static final String activityName = "com.boc.smartpos.bankpay.ui.MainActivity";

    /**
     *
     * @param p_data 消费金额(分为单位) ，长度为12，不足前补零 000000000001
     * @param mActivity 调用activity
     */
    @Override
    public void handleConsume(P_BankData p_data, Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "消费");
        intent.putExtra("amount", p_data.getAmt());
        intent.putExtra("skpServiceNo", ErpCommonData.sktno);
        intent.putExtra("skpErptransNo", ErpCommonData.orderNo);
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    @Override
    public void handleCancel(P_BankData p_data,Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "消费撤销");
        intent.putExtra("oldTrace", p_data.getOrgTraceNo());
        intent.putExtra("skpServiceNo", ErpCommonData.sktno);
        intent.putExtra("skpErptransNo", ErpCommonData.orderNo);
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    /**
     *
     * @param mActivity
     */
    @Override
    public void handleReturn(P_BankData p_data,Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "退货");
        intent.putExtra("amount", p_data.getAmt());
        intent.putExtra("skpServiceNo", ErpCommonData.sktno);
        intent.putExtra("skpErptransNo", ErpCommonData.orderNo);
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    @Override
    public void handleSign(Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "签到");
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    @Override
    public void handleSettle(Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "结算");
        intent.putExtra("skpServiceNo", ErpCommonData.sktno);
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    @Override
    public void handleQueryBalance(Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "余额查询");
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    @Override
    public void handleReprint(P_BankData p_data,Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "重打印任意一笔");
        intent.putExtra("traceNo", p_data.getTraceNo());
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    @Override
    public void handleQueryTransactionDetails(Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "交易查询");
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    @Override
    public void handleQueryTransactionSummary(Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "交易查询");
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    @Override
    public void handlePrintTransDetails(Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "打印");
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    @Override
    public void handlePrintTransSummary(Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "打印");
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    @Override
    public void handleBankData(R_BankData r_data, Object retObject) {
        try{
            Intent ret = (Intent) retObject;
            Bundle data = ret.getExtras();
            MessageLog.writeLog("BOC_银行交易返回的数据如下：");
            if (data != null) {
                for(String key:data.keySet()){
                    MessageLog.writeLog("key="+key+",   values=" + data.getString(key));
                }
            }
            MessageLog.writeLog("BOC_银行交易返回的数据结束\n");
            r_data.setResCode("0");
            r_data.setResMsg("交易成功");
            r_data.setAmt(CommonUtils.handleStringNull(data.getString("amount")));
            r_data.setTraceNo(CommonUtils.handleStringNull(data.getString("traceNo")));
            r_data.setRefNo(CommonUtils.handleStringNull(data.getString("referenceNo")));
            r_data.setCardNo(CommonUtils.handleStringNull(data.getString("cardNo")));
            r_data.setCardIssuer(CommonUtils.handleStringNull(data.getString("issue")));
            r_data.setBatchNo(CommonUtils.handleStringNull(data.getString("batchNo")));
            r_data.setTransTime(CommonUtils.handleStringNull(data.getString("time")));
            r_data.setTransDate(CommonUtils.handleStringNull(data.getString("date")));
            r_data.setTerminalID(CommonUtils.handleStringNull(data.getString("terminalId")));
            r_data.setMerchantID(CommonUtils.handleStringNull(data.getString("merchantId")));
            r_data.setMerchantName(CommonUtils.handleStringNull(data.getString("merchantName")));
            r_data.setAuthCode(CommonUtils.handleStringNull(data.getString("authCode")));
            r_data.setOrderNo(CommonUtils.handleStringNull(data.getString("orderNo")));

            if(r_data.getCardNo().equals(""))
                r_data.setCardNo("0000000000");
            long amt = Long.parseLong(r_data.getAmt());
            if (amt <= 0){
                r_data.setResCode("-1");
                r_data.setResMsg("交易失败");
                MessageLog.writeLog("BOC_中行返回金额<=0");
            }
            r_data.setAmt(MoneyUtils.getMoneyStr(amt));

            MessageLog.writeLog("BOC_中行r_data【" + r_data.toString()+"】");
        }catch (Exception e){
            r_data.setResCode("-1");
            r_data.setResMsg("交易异常");
            MessageLog.writeLog("BOC_银行交易返回处理数据异常：" + e.getMessage());
        }
    }

    /**
     * 微信扫码消费
     * @param mActivity
     */
    public void handleWeChatScanConsume(P_BankData p_data,Activity mActivity){
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "微信扫码消费");
        intent.putExtra("amount", p_data.getAmt());
        intent.putExtra("skpServiceNo", ErpCommonData.sktno);
        intent.putExtra("skpErptransNo",ErpCommonData.orderNo);
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    /**
     * 微信扫码撤销
     * @param p_data 非必须
     * @param mActivity
     */
    public void handleWeChatScanCancel(P_BankData p_data,Activity mActivity){
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "微信扫码撤销");
        intent.putExtra("skpServiceNo", ErpCommonData.sktno);
        intent.putExtra("skpErptransNo", ErpCommonData.orderNo);
//        intent.putExtra("oldOrderNo", p_data.getOrgTraceNo());
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    /**
     *
     * @param p_data
     * @param mActivity
     */
    public void handleWeChatScanReturn(P_BankData p_data,Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "微信扫码退货");
        intent.putExtra("amount", p_data.getAmt());
        intent.putExtra("skpServiceNo", ErpCommonData.sktno);
        intent.putExtra("skpErptransNo", ErpCommonData.orderNo);
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }

    public void handleWeChatScanQueryResult(P_BankData p_data,Activity mActivity) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName,activityName));
        intent.putExtra("transName", "微信支付结果查询");
        intent.putExtra("oldOrderNo", p_data.getOrderNo());
        intent.putExtra("skpServiceNo", ErpCommonData.sktno);
        intent.putExtra("skpErptransNo", ErpCommonData.orderNo);
        mActivity.startActivityForResult(intent, BankFunctionConstant.THIRD_APP_BANK_TRADE);
    }


}
